import { Component, OnInit, Injector, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { BatchCostServiceProxy, SimpleCostListDto } from '@shared/service-proxies/service-proxies';
import { TreeNode, LazyLoadEvent } from 'primeng/api';
import { Table } from 'primeng/table';
import { Paginator } from 'primeng/primeng';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { ActivatedRoute } from '@angular/router';
@Component({
    templateUrl: './cost-query-detail.component.html',
    animations: [appModuleAnimation()]
})
export class CostQueryDetailComponent extends AppComponentBase implements OnInit {
    @ViewChild('dataTable') dataTable: Table;
    @ViewChild('paginator1') paginator: Paginator;

    batch;

    constructor(
        injector: Injector,
        private _batchCostService: BatchCostServiceProxy,
        private activatedRoute: ActivatedRoute
    ) {
        super(injector);
        activatedRoute.queryParams.subscribe(queryParams => {
            this.batch = { id: queryParams.id, quantity: queryParams.quantity };
            this.batchId = this.batch.id;
            this.quantity = this.batch.quantity;
        });
    }

    batchId: string;
    quantity: number;
    haveChile: number;
    selectedTreeNode: TreeNode;
    showType = 0;

    simpleCosts: Array<SimpleCostListDto>;

    ngOnInit(): void {
        this.showType = 0;
    }

    getOwnCosts(event: TreeNode) {
        if (!event) {
            return;
        }
        this.batchId = event.data.partId;
        this.quantity = event.data.partQuantity;
        this.selectedTreeNode = event;
        this.showType = 1;
        this._batchCostService.getSimpleCosts(this.quantity, this.showType === 2, this.batchId).subscribe(result => {
            this.simpleCosts = result.items;
        });

        this.getCostDetail();
    }

    getOwnAndChildrenCosts(event: TreeNode) {
        if (!event) {
            return;
        }
        this.batchId = event.data.partId;
        this.quantity = event.data.partQuantity;
        this.selectedTreeNode = event;
        this.showType = 2;

        this._batchCostService.getSimpleCosts(this.quantity, this.showType === 2, this.batchId).subscribe(result => {
            this.simpleCosts = result.items;
        });
        this.getCostDetail();
    }

    getCostDetail(event?: LazyLoadEvent) {
        if (!this.showType) {
            return;
        }
        if (this.primengTableHelper.shouldResetPaging(event)) {
            this.paginator.changePage(0);

            return;
        }
        this.primengTableHelper.showLoadingIndicator();
        this._batchCostService
            .getCostDetails(
                this.quantity,
                this.showType === 2,
                this.batchId,
                null,
                this.primengTableHelper.getMaxResultCount(this.paginator, event),
                this.primengTableHelper.getSkipCount(this.paginator, event)
            )
            .subscribe(result => {
                this.primengTableHelper.totalRecordsCount = result.totalCount;
                this.primengTableHelper.records = result.items;
                this.primengTableHelper.hideLoadingIndicator();
            });
    }
}
